﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nRoute.Behaviors;
using nRoute.Navigation;

namespace TabsNavigation.Behaviors
{
    public class TabControlNavigationAdapter : NavigationAdapterBehaviorBase<TabControl>
    {
        public override object Content
        {
            get 
            {
                if (this.AssociatedObject.Items.Count > 0)
                {
                    foreach (TabItem _tabItem in this.AssociatedObject.Items)
                    {
                        if (_tabItem.IsSelected)
                        {
                            return _tabItem.Content;
                        }
                    }
                }
                return null;
            }
        }

        protected override void OnSetNavigationContent(object content)
        {
            var _tabItem = new TabItem();
            _tabItem.Content = content;
            _tabItem.Header = Navigation.GetTitle(content as DependencyObject) ?? "Untitled";

            this.AssociatedObject.Items.Add(_tabItem);
            _tabItem.IsSelected = true;
        }
    }
}
